<script setup lang="ts">
import { Clock } from '@element-plus/icons-vue'
import { ref } from 'vue'
import { useWatchHistry, type typeWatchHistry } from '@/store/watchHistry'
import { useUserState } from '@/store/userState'
const userState = useUserState()

const watchHistry = useWatchHistry()
watchHistry.setListFromServe()
const showPopover = ref(false)
function mouseenter() {
  showPopover.value = true
}

function mouseout() {
  showPopover.value = false
}

const showLoginBox = ref(false)
async function toHistry() {
  try {
    if (await userState.getUserInfo()) {
      console.log('new')
      window.open('/user/histry')
      return
    }
  } catch (error) {
    // 获取token失败
  }
  showLoginBox.value = true
}

function toHref(url: string) {
  window.open(url)
}
</script>

<template>
  <div class="box">
    <el-icon class="icon" size="30px"><Clock /></el-icon>

    <div class="popover" @mouseenter="mouseenter" @mouseleave="mouseout" :class="{ popoverShow: showPopover }" @click="toHistry">
      <Transition>
        <div class="popoverBox" v-if="showPopover">
          <div style="width: 250px; text-align: center" v-show="JSON.stringify(watchHistry.list) == '{}'">
            <el-empty description="暂无观看历史" />
          </div>
          <el-timeline>
            <template v-for="v in watchHistry.getList" :key="v.name">
              <el-timeline-item v-if="v.list.length > 0" :timestamp="v.name" placement="top">
                <template v-for="info in v.list" :key="info.vod_id">
                  <div class="videoCard" @click.stop="toHref(`/play/${info.vod_id}?episode=${info.episode}`)">
                    <el-image :src="info.vod_img" fit="cover" />
                    <div class="videoCard-vBox">
                      <p class="videoCard-vBox-title">{{ info.vod_name }}</p>
                      <p>{{ info.episode_name }} 观看至{{ ((info.currentTime / info.duration) * 100).toFixed(0) }}%</p>
                    </div>
                  </div>
                </template>
              </el-timeline-item>
            </template>
          </el-timeline>
        </div>
      </Transition>
    </div>
    <login @close="showLoginBox = false" :show="showLoginBox"></login>
  </div>
</template>

<style scoped lang="less">
.el-timeline {
  max-height: 280px;
  overflow-y: auto;
  padding: 2px;
  padding-bottom: 0;

  // 滚动条宽度
  &::-webkit-scrollbar {
    width: 6px;
  }
  // 滚动条轨道
  &::-webkit-scrollbar-track {
    background: rgba(0, 0, 0, 0);
    border-radius: 2px;
  }

  &::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, 0);
    border-radius: 10px;
  }

  &:hover {
    &::-webkit-scrollbar-thumb {
      background: rgba(255, 255, 255, 0.2);
    }
  }
}
.videoCard {
  display: flex;
  align-items: center;
  margin-bottom: 5px;
  margin-right: 10px;
  cursor: pointer;
  padding: 5px 10px 5px 5px;

  &:hover {
    background: rgba(255, 255, 255, 0.1);
    border-radius: 10px;
  }

  .el-image {
    width: 65px;
    height: 80px;
    border-radius: 10px;
    margin-right: 10px;
  }
  &-vBox {
    color: #999;

    &-title {
      font-size: 17px;
      font-weight: bold;
      margin-bottom: 20px;
      width: 150px;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      color: #fff;
    }
  }
}

.v-enter-active,
.v-leave-active {
  transition: 0.2s ease;
}

.v-enter-from,
.v-leave-to {
  opacity: 0;
}

.popover {
  // display: none;
  position: absolute;
  right: 0;
  margin-right: 95px;

  .popoverBox {
    padding: 20px 10px;
    border-radius: 10px;
    margin-top: 20px;
    background: rgba(0, 0, 0, 0.9);
    position: relative;
    margin-right: -60px;

    &::before {
      display: block;
      content: '';
      width: 0;
      height: 0;
      border-bottom: 10px solid rgba(0, 0, 0, 0.9);
      border-right: 10px solid transparent;
      border-left: 10px solid transparent;
      position: absolute;
      top: -10px;
      right: 90px;
    }
  }

  &::after {
    display: block;
    content: '';
    position: absolute;
    top: -50px;
    right: 10px;
    // background: #f00;
    width: 60px;
    height: 70px;
    cursor: pointer;
  }
}
.icon {
  cursor: pointer;
  color: rgba(255, 255, 255, 0.5);

  &:hover {
    color: rgb(1, 206, 205);
  }
}

.router-link-active {
  text-decoration: none;
}

a {
  text-decoration: none;
}
</style>
